<script setup>
import {
    Edit,
    Delete
} from '@element-plus/icons-vue'
import { ref } from 'vue'
//数据模型 一个数组三个js对象
const family = ref([])
//声明一个异步的函数 页面加载完成就得调用方法展示查询结果 调用函数时需要同步等待返回结果
import {familyListService,familyAddService,familyUpdateService,familyDeleteService} from '@/api/article.js'
const familyList = async() =>{
    let result = await familyListService()
    //没有问题就返回文章分类列表 后端返回数据data数组类型
    family.value = result.data
}
familyList();//调用
//401没有携带token过来访问后台时 向后台发送请求时需携带token


//控制添加分类弹窗 控制弹窗显示默认false
const dialogVisible = ref(false)

//添加分类数据模型
const familyModel = ref({
    familyNumber: '',
    familyIncome: '',
    familyStuff:'',
    familyStatus:'',
    createtime:''
})
//添加分类表单校验
const rules = {
    familyNumber: [
        { required: true, message: '请输入家庭人数', trigger: 'blur' },
    ],
    familyIncome: [
        { required: true, message: '请输入家庭收入', trigger: 'blur' },
    ]
}


//调用接口添加表单
import { ElMessage } from 'element-plus'
const addFamily = async() =>{
    //调用接口
    let result = await familyAddService(familyModel.value)
    ElMessage.success(result.msg?result.msg:'添加成功')

    //刷新一下 获取所有文章分类
    familyList();
    dialogVisible.value=false;
}

//定义变量控制标题的展示
const title = ref('')

//展示编辑弹窗 row用来接收点击编辑时这一行的数据
//一点击编辑按钮，familyModel.value的各项就自动=row的各项 再把familyModel传给updateFamily就可给后台完成修改
const showDialog = (row) =>{
    dialogVisible.value = true;//放进标签里不用.value
    title.value='编辑家庭'
    //数据拷贝 修改时的回写也就是展示出来在这一行的数据在修改弹窗上
    familyModel.value.familyNumber=row.familyNumber;
    familyModel.value.familyIncome=row.familyIncome;
    familyModel.value.familyId = row.familyId;//扩展id属性将来传递给后台完成修改

}

//编辑家庭
const updateFamily = async()=>{
    //调用接口
    let result = await familyUpdateService(familyModel.value)
    ElMessage.success(result.msg?result.msg:'修改成功 ')

    familyList();
    dialogVisible.value=false;
}

//清空模型的数据 防止编辑之后添加家庭会数据回写 点击添加家庭时数据清空
const clearData = () =>{
    familyModel.value.familyNumber='',
    familyModel.value.familyIncome=''
}

import { ElMessageBox } from 'element-plus'
const deleteFamily = (row) =>{
    //提示用户 cv
    ElMessageBox.confirm(
    '确认删除吗?',
    '提示',
    {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
    }
    )
    .then(async() => {
        //调用接口
        let result = await familyDeleteService(row.familyId);
    ElMessage({
        type: 'success',
        message: '删除成功',
    })
    //刷新列表
    familyList();
    })
    .catch(() => {
    ElMessage({
        type: 'info',
        message: '删除取消',
    })
    })
    //cv

    }




const currentAuditId = ref('')
const auditDialogVisible = ref(false)

// 显示审核确认弹窗
const showAuditConfirm = (row, isPass) => {
  currentAuditId.value = row.familyId
  ElMessageBox.confirm(
    `确认要${isPass ? '通过' : '驳回'}该家庭的补助申请吗？`,
    '审核确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: isPass ? 'success' : 'warning'
    }
  )
  .then(async () => {
    // 调用更新接口修改审核状态
    const result = await familyUpdateService({
      familyId: currentAuditId.value,
      familyStatus: isPass ? '已通过' : '不通过'
    })
    ElMessage.success(`审核${isPass ? '通过' : '驳回'}成功`)
    familyList() // 刷新列表
  })
  .catch(() => {
    ElMessage.info('已取消审核')
  })
}



</script>
<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>家庭补助</span>
                <div class="extra">
                    <el-button type="primary" @click="dialogVisible=true; title='添加家庭'; clearData()">添加家庭</el-button>
                </div>
            </div>
        </template>

        <!-- 家庭列表表格 -->
        <el-table :data="family" style="width: 100%">
            <el-table-column label="序号" width="100" type="index"></el-table-column>
            <el-table-column label="家庭人数" prop="familyNumber"></el-table-column>
            <el-table-column label="家庭收入" prop="familyIncome"></el-table-column>
            <el-table-column label="家庭补助金额" prop="familyAmount"></el-table-column>
            <el-table-column label="家庭资料" prop="familyStuff"></el-table-column>
            <el-table-column label="申请时间" prop="createTime">
                <template #default="{ row }">
                    {{ row.createTime ? new Date(row.createTime).toLocaleString() : '-' }}
                </template>
            </el-table-column>
            <el-table-column label="审核状态" prop="familyStatus">
                <template #default="{ row }">
                    <el-tag 
                        :type="row.familyStatus === '已通过' ? 'success' : 
                               row.familyStatus === '不通过' ? 'danger' : 'warning'"
                    >
                        {{ row.familyStatus }}
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="300">
                <template #default="{ row }">
                    <el-button :icon="Edit" circle plain type="primary" @click="showDialog(row)"></el-button>
                    <el-button :icon="Delete" circle plain type="danger" @click="deleteFamily(row)"></el-button>
                    
                    <!-- 审核按钮：仅待审核状态显示 -->
                    <template v-if="row.familyStatus === '待审核'">
                        <span class="operation-divider">|</span>
                        <el-button type="success" plain size="small" @click="showAuditConfirm(row, true)">
                            审核通过
                        </el-button>
                        <el-button type="danger" plain size="small" @click="showAuditConfirm(row, false)">
                            审核不通过
                        </el-button>
                    </template>
                </template>
            </el-table-column>
            <template #empty>
                <el-empty description="没有数据" />
            </template>
        </el-table>

        <!-- 添加/编辑弹窗 -->
        <el-dialog v-model="dialogVisible" :title="title" width="30%">
            <el-form :model="familyModel" :rules="rules" label-width="100px" style="padding-right: 30px">
                <el-form-item label="家庭人数" prop="familyNumber">
                    <el-input v-model="familyModel.familyNumber" minlength="1" maxlength="10"></el-input>
                </el-form-item>
                <el-form-item label="家庭收入" prop="familyIncome">
                    <el-input v-model="familyModel.familyIncome" minlength="1" maxlength="15"></el-input>
                </el-form-item>
                <el-form-item label="家庭资料" prop="familyStuff">
                    <el-input v-model="familyModel.familyStuff" maxlength="200"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="title === '添加家庭' ? addFamily() : updateFamily()">确认</el-button>
                </span>
            </template>
        </el-dialog>
    </el-card>
</template>

<style lang="scss" scoped>
.page-container {
    min-height: 100%;
    box-sizing: border-box;

    .header {
        display: flex;
        align-items: center;
        justify-content: space-between;
    }
}
</style>